package com.hnjowe.webdb.action;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.organization.model.Personnel;
import com.union.core.organization.service.PersonnelService;

public class ExcelAction extends ActionSupport {
	private Personnel personal;
	private List<Personnel> personalList;
	private PersonnelService perService;
	public ExcelAction(){
		perService=new PersonnelService();
		personalList=new ArrayList<Personnel>();
		
	}
	public String outModel(){
		personalList=perService.queryList(0, null, null,
				null, null, null, null,
				null, null, null, 0, 5, 
				0, null);
		for(Personnel per:personalList){
			postSend("http://127.0.0.1/OutFromModel","personal="+per);
		}
		
		return SUCCESS;
	}
	public static String postSend(String strUrl,String param){
		
		
		URL url = null;
		HttpURLConnection connection = null;
		
		try {
			url = new URL(strUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.connect();

			//POST方法时使用
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.writeBytes(param);
			out.flush();
			out.close();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
		
			reader.close();
			return buffer.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (connection != null) {
				connection.disconnect();
				}
		}
		
		
	}
	public Personnel getPersonal() {
		return personal;
	}
	public void setPersonal(Personnel personal) {
		this.personal = personal;
	}
}
